package com.jdz.controller.backend;

import com.jdz.common.Const;
import com.jdz.common.ServerResponse;
import com.jdz.pojo.Announcemen;
import com.jdz.service.IAnnouncemenService;
import com.jdz.service.IUserService;
import com.jdz.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
//接收数组胡时候参数要加@RequestParam(value = "annnoList"，不然错误

/**
 * Created by sun
 **/
@Controller
@RequestMapping("/manage/announcemen")
public class AnnouncemenManageController {

    @Autowired
    IUserService iUserService;
    @Autowired
    IAnnouncemenService iAnnouncemenService;



    /**
     * 添加或者更新公告
     * @param session
     * @param announcemen
     * @return
     */
    @RequestMapping(value = "save.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse announcemenSave(HttpSession session, Announcemen announcemen) {

        UserVo userVo = (UserVo) session.getAttribute(Const.CURRENT_USER);

        announcemen.setUserno(userVo.getStuno());
        return iAnnouncemenService.saveOrUpdateAnnouncemen(announcemen);

    }

    /**
     * 置顶公告
     * @param session
     * @param annnoList
     * @return
     */
    @RequestMapping(value = "announcemen_top.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse announcemenTop(HttpSession session, @RequestParam(value = "annnoList[]")List<Integer>  annnoList) {

        return iAnnouncemenService.announcemenTop(annnoList);

    }


    /**
     * 取消置顶公告
     * @param session
     * @param annnoList
     * @return
     */
    @RequestMapping(value = "announcemen_remove_top.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse announcemenRemoveTop(HttpSession session, @RequestParam(value = "annnoList[]")List<Integer>  annnoList) {
        return iAnnouncemenService.announcemenRemoveTop(annnoList);
    }

    /**
     * 删除公告
     * @param request
     * @param session
     * @param annnoList
     * @return
     */
    @RequestMapping(value = "delete_announcemen.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse deleteAnnouncemen(HttpServletRequest request, HttpSession session, @RequestParam(value = "annnoList[]") List<Integer>  annnoList) {

        return iAnnouncemenService.deleteAnnouncemen(annnoList);

    }

//    /**
//     * 后台公告列表
//     * @param request
//     * @param session
//     * @param pageNum
//     * @param pageSize
//     * @return
//     */
//    @RequestMapping(value = "list_announcemen.do",method = RequestMethod.POST)
//    @ResponseBody
//    public ServerResponse  listAnnouncemen(HttpServletRequest request, HttpSession session,@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
//                                           @RequestParam(value = "pageSize",defaultValue = "5")int pageSize) {
//
//        UserVo userVo = (UserVo) session.getAttribute(Const.CURRENT_USER);
//        String url = request.getRequestURL().toString();
//        if (userVo == null) {
//            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "管理员未登录,请登录");
//        }
//        //校验一下是否是管理员
//        if (iUserService.checkAdminRole(userVo).isSuccess()) {
//            //是管理员
//            if (iUserService.checkPrivilege(userVo, url).isSuccess()) {
//                //增加我们处理逻辑
//                return iAnnouncemenService.listAnnouncemen(pageNum,pageSize);
//            } else {
//                return ServerResponse.createByErrorMessage("无权限操作,该管理员无此权限");
//            }
//        } else {
//            return ServerResponse.createByErrorMessage("无权限操作,需要管理员权限");
//        }
//    }

    /**
     * 公告详情
     * @param request
     * @param session
     * @param annno
     * @return
     */
    @RequestMapping(value = "details_announcemenn.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse detailsAnnouncemen(HttpServletRequest request, HttpSession session, Integer  annno) {
        return iAnnouncemenService.detailsAnnouncemen(annno);
    }

    /**
     * 关键字搜索
     * @param name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "search_announcemenn.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse searchAnnouncemen(@RequestParam(value = "name",required = false)
            String name, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                            @RequestParam(value = "pageSize",defaultValue = "5")int pageSize
            , @RequestParam(value = "time_sort",defaultValue = "0") int time_sort
                                             ) {
        return iAnnouncemenService.searchAnnounceme(name,pageNum,pageSize,time_sort);
    }




}
